package org.erp.spu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.erp.common.page.PageQueryModel;
import org.erp.spu.entity.SpuReleaseGroup;
import org.erp.spu.entity.bo.SpuReleaseGroupBo;
import org.erp.spu.entity.dto.SpuReleaseGroupDto;
import org.erp.spu.entity.vo.ReleaseGroupVo;
import org.erp.spu.entity.vo.SpuReleaseGroupVo;
import org.erp.spu.mapper.SpuReleaseGroupMapper;
import org.erp.spu.mapper.SpuReleaseInfoMapper;
import org.erp.spu.service.SpuReleaseGroupService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SpuReleaseGroupServiceImpl extends ServiceImpl<SpuReleaseGroupMapper, SpuReleaseGroup> implements SpuReleaseGroupService {

    @Resource
    private SpuReleaseGroupMapper spuReleaseGroupMapper;
    @Resource
    private SpuReleaseInfoMapper spuReleaseInfoMapper;

    @Override
    public List<SpuReleaseGroupVo> getList(SpuReleaseGroupBo spuReleaseGroupBo) {
        LambdaQueryWrapper<SpuReleaseGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(spuReleaseGroupBo.getGroupName()), SpuReleaseGroup::getGroupName, spuReleaseGroupBo.getGroupName());
        queryWrapper.eq(StringUtils.hasText(spuReleaseGroupBo.getStatus()), SpuReleaseGroup::getStatus, spuReleaseGroupBo.getStatus());
        queryWrapper.eq(spuReleaseGroupBo.getCategoryId()!=null, SpuReleaseGroup::getCategoryId, spuReleaseGroupBo.getCategoryId());
        queryWrapper.eq(SpuReleaseGroup::getDelFlag, 0);
        List<SpuReleaseGroup> list = spuReleaseGroupMapper.selectList(queryWrapper);
        return list.stream().map(this::convertToVo).collect(Collectors.toList());
    }

    @Override
    public PageQueryModel<SpuReleaseGroupVo> getListByPage(SpuReleaseGroupDto spuReleaseGroupDto) {
        LambdaQueryWrapper<SpuReleaseGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(spuReleaseGroupDto.getGroupName()), SpuReleaseGroup::getGroupName, spuReleaseGroupDto.getGroupName());
        queryWrapper.eq(StringUtils.hasText(spuReleaseGroupDto.getStatus()), SpuReleaseGroup::getStatus, spuReleaseGroupDto.getStatus());
        queryWrapper.eq(spuReleaseGroupDto.getCategoryId()!=null, SpuReleaseGroup::getCategoryId, spuReleaseGroupDto.getCategoryId());
        queryWrapper.eq(SpuReleaseGroup::getDelFlag, 0);
        Page<SpuReleaseGroup> page = this.page(spuReleaseGroupDto.buildPage(), queryWrapper);
        List<SpuReleaseGroupVo> voList = page.getRecords().stream().map(this::convertToVo).collect(Collectors.toList());
        return PageQueryModel.of(page.getPages(), voList);
    }

    @Override
    public boolean addSpuReleaseGroup(SpuReleaseGroupDto spuReleaseGroupDto) {
        Assert.isTrue(!checkGroupName(spuReleaseGroupDto), "发布组名称已存在");
        SpuReleaseGroup entity = new SpuReleaseGroup();
        BeanUtils.copyProperties(spuReleaseGroupDto, entity);
        return spuReleaseGroupMapper.insert(entity) > 0;
    }

    @Override
    public boolean updateSpuReleaseGroup(SpuReleaseGroupDto spuReleaseGroupDto) {
        Assert.isTrue(!checkGroupName(spuReleaseGroupDto), "发布组名称已存在");
        SpuReleaseGroup entity = new SpuReleaseGroup();
        BeanUtils.copyProperties(spuReleaseGroupDto, entity);
        return spuReleaseGroupMapper.updateById(entity) > 0;
    }

    @Override
    public SpuReleaseGroupVo getSpuReleaseGroupVoById(Long id) {
        SpuReleaseGroup entity = spuReleaseGroupMapper.selectById(id);
        return convertToVo(entity);
    }

    /**
     * 根据品类ID获取发布组列表
     * @param categoryId 品类ID
     * @return 发布组列表
     */
     @Override
     public List<ReleaseGroupVo> getListByCategoryId(Long categoryId) {
        return spuReleaseGroupMapper.getListByCategoryId(categoryId);
    }
//    @Override
//    public List<ReleaseGroupVo> getListByCategoryId(Long categoryId) {
//        List<SpuReleaseGroup> groupList = spuReleaseGroupMapper.selectList(new LambdaQueryWrapper<SpuReleaseGroup>().eq(SpuReleaseGroup::getCategoryId, categoryId).eq(SpuReleaseGroup::getDelFlag, 0));
//        List<ReleaseGroupVo> releaseGroupVoList = groupList.stream().map(group -> {
//            ReleaseGroupVo releaseGroupVo=new ReleaseGroupVo();
//            releaseGroupVo.setGroupId(group.getId());
//            releaseGroupVo.setGroupName(group.getGroupName());
//            List<SpuReleaseInfo>  parentlist=spuReleaseInfoMapper.selectList(new LambdaQueryWrapper<SpuReleaseInfo>()
//                    .eq(SpuReleaseInfo::getReleaseGroupId, group.getId()).eq(SpuReleaseInfo::getType,1)
//                    .eq(SpuReleaseInfo::getDelFlag, 0));
//            releaseGroupVo.setReleaseList(parentlist.stream().map(parent -> {
//                ReleaseVo releaseVo=new ReleaseVo();
//                releaseVo.setParentId(parent.getId());
//                releaseVo.setParentName(parent.getName());
//                List<SpuReleaseInfo> childrenList=spuReleaseInfoMapper.selectList(
//                        new LambdaQueryWrapper<SpuReleaseInfo>()
//                                .eq(SpuReleaseInfo::getParentId, parent.getId()).eq(SpuReleaseInfo::getReleaseGroupId, group.getId())
//                                .eq(SpuReleaseInfo::getType,0)
//                                .eq(SpuReleaseInfo::getDelFlag, 0));
//                releaseVo.setChildrenList(childrenList.stream().map(children -> {
//                    ReleaseChildrenVo childrenVo=new ReleaseChildrenVo();
//                    childrenVo.setChildrenId(children.getId());
//                    childrenVo.setChildrenName(children.getName());
//                    return childrenVo;
//                }).collect(Collectors.toList()));
//                return releaseVo;
//            }).collect(Collectors.toList()));
//            return releaseGroupVo;
//        }).collect(Collectors.toList());
//
//        return releaseGroupVoList;
//    }

    private SpuReleaseGroupVo convertToVo(SpuReleaseGroup entity) {
        SpuReleaseGroupVo vo = new SpuReleaseGroupVo();
        if (entity != null) {
            BeanUtils.copyProperties(entity, vo);
        }
        return vo;
    }

    private boolean checkGroupName(SpuReleaseGroupDto spuReleaseGroupDto) {
        if (!StringUtils.hasText(spuReleaseGroupDto.getGroupName())) {
            return false;
        }
        LambdaQueryWrapper<SpuReleaseGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuReleaseGroup::getGroupName, spuReleaseGroupDto.getGroupName());
        queryWrapper.eq(SpuReleaseGroup::getCategoryId, spuReleaseGroupDto.getCategoryId());
        queryWrapper.eq(SpuReleaseGroup::getDelFlag, 0);
        if (spuReleaseGroupDto.getId() != null) {
            queryWrapper.ne(SpuReleaseGroup::getId, spuReleaseGroupDto.getId());
        }
        return this.count(queryWrapper) > 0;
    }
}
